package com.yorick.authority.controller;

import com.yorick.authority.enums.Account;
import com.yorick.authority.modal.request.department.*;
import com.yorick.authority.service.DepartmentService;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 机构管理
 * @author: Yorick
 * @create: 2022-05-16 12:52
 **/
@Slf4j
@Controller
@RequestMapping("department")
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    //新增机构
    @RequestMapping(value = "addDepartment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> addDepartment(@Valid @RequestBody DepartmentAddRequest requestBody,
                                             HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        return departmentService.addDepartment(requestBody);
    }

    //机构列表
    @RequestMapping(value = "departmentList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> departmentList(@Valid @RequestBody DepartmentListRequest requestBody,
                                              HttpServletRequest request) {
        HttpSession session = request.getSession();
        requestBody.setAid(Integer.parseInt(session.getAttribute(Account.aid.name()).toString()));
        requestBody.setSuperRole(Boolean.parseBoolean(session.getAttribute(Account.superRole.name()).toString()));
        return ResultFactory.success(departmentService.departmentList(requestBody)).display();
    }

    //编辑机构
    @RequestMapping(value = "editDepartment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> editDepartment(@Valid @RequestBody DepartmentEditRequest requestBody) {
        return departmentService.editDepartment(requestBody);
    }

    //机构详情
    @RequestMapping(value = "departmentDetail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> departmentDetail(@Valid @RequestBody DepartmentDetailRequest requestBody) {
        return ResultFactory.success(departmentService.departmentDetail(requestBody)).display();
    }

    //删除机构
    @RequestMapping(value = "deleteDepartment", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> deleteDepartment(@Valid @RequestBody DepartmentDeleteRequest requestBody) {
        return departmentService.deleteDepartment(requestBody);
    }
}
